package com.xyp.iodemo.nettyrpcclient.netty;

import com.xyp.iodemo.nettyrpcclient.interfaces.INettyCallBack;
import com.xyp.iodemo.nettyrpccommon.model.RPCNetty;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @author xuyuanpeng
 * @version 1.0
 * @date 2019-03-06 17:52
 */
public class EchoClient {
    private  String host;
    private  int port;
    private InetSocketAddress inetSocketAddress;
    private INettyCallBack nettyCallBack;
    private RPCNetty rpcNetty;
    private static java.util.logging.Logger logger = java.util.logging.Logger.getLogger("EchoClient");

    public void start() throws Exception{
        EventLoopGroup group=new NioEventLoopGroup();
        try {
            Bootstrap b=new Bootstrap();
            b.group(group).channel(NioSocketChannel.class)
                    .remoteAddress(inetSocketAddress)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            EchoClientHandler echoClientHandler=new EchoClientHandler();
                            echoClientHandler.setRpcNetty(rpcNetty);
                            echoClientHandler.setNettyCallBack(nettyCallBack);;

                            socketChannel.pipeline().addLast(echoClientHandler);
                        }
                    });

            ChannelFuture future=b.connect().sync();
            future.channel().closeFuture().sync();
        }finally {
            group.shutdownGracefully().sync();
        }
    }


    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public InetSocketAddress getInetSocketAddress() {
        return inetSocketAddress;
    }

    public void setInetSocketAddress(InetSocketAddress inetSocketAddress) {
        this.inetSocketAddress = inetSocketAddress;
    }

    public void setRpcNetty(RPCNetty rpcNetty) {
        this.rpcNetty = rpcNetty;
    }

    public RPCNetty getRpcNetty() {
        return rpcNetty;
    }

    public INettyCallBack getNettyCallBack() {
        return nettyCallBack;
    }

    public void setNettyCallBack(INettyCallBack nettyCallBack) {
        this.nettyCallBack = nettyCallBack;
    }

    public EchoClient(String host, int port){
        this.host=host;
        this.port=port;
        inetSocketAddress=new InetSocketAddress(host,port);
    }

    public EchoClient(InetSocketAddress inetSocketAddress){
        this.inetSocketAddress=inetSocketAddress;
    }
}
